package cheme.ProcessModel.Interpreter;

import java.util.HashMap;
import java.util.Map;

import cheme.ProcessModel.IModelElement;

/*
 * A common pattern in these post tests is to install a 'fail' updater that
 * looks for changes to the system in order to attemp a re-validation. For
 * instance, making reference in an equation to a non-existent tag component
 * will install a listener that looks for additions to the system with the
 * needed name.
 */
public class PostparseRuleCheckerFactory implements IRuleCheckerFactory
{
	private static IRuleCheckerFactory instance = null;
	private static Map<Class, IRuleCheckVisitor> ruleSets = null;
	
	private PostparseRuleCheckerFactory()
	{
	}
	
	public static IRuleCheckerFactory getInstance()
	{
		if(null == instance)
		{
			instance = new PostparseRuleCheckerFactory();
		}
		
		return instance;
	}
	
	private static Map<Class, IRuleCheckVisitor> getRuleSetsMap()
	{
		if(null == ruleSets)
		{
			ruleSets = new HashMap<Class, IRuleCheckVisitor>();
		}
		
		return ruleSets;
	}
	
	public IRuleCheckVisitor getRuleChecker(IModelElement me)
	{
		IRuleCheckVisitor ruleChecker = getRuleSetsMap().get(me.getClass());
		
		if(null == ruleChecker)
		{
			ruleChecker = new PostparseRuleChecker();
			
			me.accept(ruleChecker);
			
			getRuleSetsMap().put(me.getClass(), ruleChecker);
		}
		
		return ruleChecker;
	}
}
